Conditional Execution

Every day we make decisions based on certain criteria and react accordingly. For example, a student in CS 310 may decide:

If I get a B or better in CS 310, then I will be happy.

This sentence is a conditional statement.

We can represent this example in Matlab as follows:

    cs310grade = x     % where x is a number between 0 and 4 (inclusive)

    if cs310grade >= 3
        disp('I am happy')
    end

It has a condition, also called a boolean expression ("I get a B or better in CS 310") and a statement block that will be executed if the expression is true ("I will be happy"). A boolean expression evaluates to true or false, instead of a numeric value.

In the example, the variable cs310grade is the grade point value of the letter grade. If the grade point value is greater than the equivalent to a B, the command window displays the phrase "I am happy". The word end is like the period in a sentence. It indicates that the if statement is done, and the following commands, if any, are not affected by the conditional statement.

Conditional execution or else

Include an else clause in your if statement to provide an alternative code block to execute if the condition is false.

    if cs310grade >= 3
        disp('I am happy');
    else
        disp('I am sad');
    end

The words if, else and end are keywords,
and will show up in blue when typed into the Matlab text editor.
The general form of an if statement in Matlab is:

if condition
    statements  % body of the true part of the if statement
                % contains one or more statements
                % that execute if the condition is true
else
    statements  % body of the false part of the if statement
                % contains one or more statements
                % that execute if the condition is false
end

where condition is a boolean expression and statements is one
or more Matlab command statements that we want to execute if the
boolean expression, condition evaluates to true.

We represent if statements graphically
using a control flow chart with a decision control and
multiple paths, one for each possible outcome of the
condition test. This control flow diagram illustrates an if else statement that allows a program to choose between two alternate blocks of code to execute.

control flow diagram showing the true and false paths that can be followed depending upon the results of evaluating the condition clause

elseif

The elseif statement provides a way to test another condition when the preceding tests failed.

    if x == 5
         disp( 'x is 5' );
    elseif y ~= 6
         disp( 'x is not 5 and y is not equal to 6' );
    elseif height > 10
         disp( 'x is not 5 and y is equal to 6 and height is greater than 10' );
    end

When using elseif statements, it is possible that none of the statements will execute. To include a statement that executes if none of the other possibilities were executed, include an else clause (part) in your code.

    if x == 5
         disp( 'x is 5' );
    elseif y ~= 6
         disp( 'x is not 5 and y is not equal to 6' );
    elseif height > 10
         disp( 'x is not 5 and y is equal to 6 and height is greater than 10' );
    else
         disp( 'x is not 5 and y is equal to 6 and height is not greater than 10' );    
    end